﻿using Maticsoft.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Maticsoft.BLL;
using command;
using Model;
using LxMesApp1.Properties;
using System.Reflection;
namespace LxMesApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //这个错误信息表明你尝试在由 WinForms 设计器生成的 InitializeComponent 方法中添加异步事件处理器，这是不被支持的。InitializeComponent 方法中的代码是自动生成的，用于初始化窗体及其控件，因此不应该手动修改它。

            //在 WinForms 中，事件处理器通常在窗体的构造函数中添加，而不是在 InitializeComponent 方法中。以下是一个正确添加异步事件处理器的示例：
            //this.button1.Click += async (sender, e) => await Task.Run(() => button1_ClickAsync(sender, e));            
        }



        private void button2_Click(object sender, EventArgs e)
        {
            HttpClient Client = new HttpClient();

            var headers = new Dictionary<string, string>
            {
                { "Authorization", "FN6/jhuULbmg3IFolczTsNI6XpWJeRPeiKQYDe3kCzR6WBVC6KwsJTAnkJHeBmouwMQDxgd0pgpU7/qUgYHZQA==" }
            };

            string reportUrl = "http://192.168.23.167:10008/device/list";
            var paramsObject = new { /* 这里添加你的参数对象 */ };

            string createRetJson = HttpClientUtils.DoJsonPost(paramsObject, reportUrl, headers);
            Console.WriteLine(createRetJson);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            HttpClient Client = new HttpClient();

            var headers = new Dictionary<string, string>
            {
                { "Authorization", "FN6/jhuULbmg3IFolczTsNI6XpWJeRPeiKQYDe3kCzR6WBVC6KwsJTAnkJHeBmouwMQDxgd0pgpU7/qUgYHZQA==" }
            };

            string reportUrl = "http://192.168.22.250:10008/dr/list-frontend";

            DateTime dateTime = DateTime.Today.Date.AddDays(-0); // 获取当前UTC时间
            long timestamp1 = new DateTimeOffset(dateTime).ToUnixTimeMilliseconds();

            DateTime now = DateTime.Now; // 获取当前时间
            DateTime startOfDay = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0); // 当天的开始时间
            DateTime endOfLastHour = startOfDay.AddHours(23).AddMinutes(59); // 当天最后一个小时的结束时间
            // 如果你需要的是开始时间而不是结束时间，可以减去59分钟再减去1小时
            DateTime startOfLastHour = endOfLastHour.AddMinutes(-59).AddHours(-1).AddDays(-0);
            long timestamp2 = new DateTimeOffset(startOfLastHour).ToUnixTimeMilliseconds();

            List<int> ints = new List<int>();
            ints.Add(632);
            ints.Add(771);
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                DefaultValueHandling = DefaultValueHandling.Ignore
            };

            Maticsoft.BLL.TypeValue bllValue = new Maticsoft.BLL.TypeValue();

            foreach (var item in ints)
            {
                Rootdr paramsObject = new Rootdr
                {
                    currentPage = 0, // 当前分页页数
                    orderBy = null, // 排序的字段  如 timestamp
                    orderDirection = null, // desc | asc
                    customizedPageSize = 20, //自定义分页数 默认20
                    sortOrders = null,
                    stationNumber = item, // 站号 （必填）
                    start = timestamp1, // 开始时间戳
                    end = timestamp2, // 结束时间戳
                };
                string createRetJson = HttpClientUtils.DoJsonPost(paramsObject, reportUrl, headers);
                Console.WriteLine(createRetJson);
                createRetJson = createRetJson.Replace(" ", "");
                Root data = JsonConvert.DeserializeObject<Root>(createRetJson);


                if (data.code == 0 && data.results.Count > 0)
                {
                    Maticsoft.Model.TypeValue data1 = new Maticsoft.Model.TypeValue();

                    Maticsoft.BLL.listfrontend bll = new Maticsoft.BLL.listfrontend();
                    string aaa = DateTime.Now.AddDays(-0).ToString("yyyy-MM-dd");
                    bool ii = bll.Delete(aaa, item);
                    bool delete = bllValue.Delete1(aaa, item);

                    Maticsoft.Model.listfrontend listfrontend = new Maticsoft.Model.listfrontend();
                    listfrontend.accumulateVolume = Convert.ToDecimal(data.results[0].accumulateVolume);
                    listfrontend.electricityCosume = Convert.ToDecimal(data.results[0].electricityCosume);
                    listfrontend.code = data.code;
                    listfrontend.message = data.message;
                    listfrontend.stationNumber = data.pager.stationNumber;
                    listfrontend.timestamp = data.results[0].timestamp;
                    long timestamp = data.results[0].timestamp;
                    DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
                    DateTime localDateTime = dateTimeOffset.LocalDateTime;
                    listfrontend.displayTime = Convert.ToDateTime(localDateTime);
                    listfrontend.count = data.results[0].count;
                    listfrontend.vendor = data.results[0].vendor;
                    listfrontend.bitPart = data.results[0].bitPart;
                    listfrontend.deviceId = data.results[0].deviceId;
                    listfrontend.valueMapping = data.results[0].valueMapping.ToJSON();

                    data1 = JsonConvert.DeserializeObject<Maticsoft.Model.TypeValue>(listfrontend.valueMapping, settings);

                    data1.count = listfrontend.count;
                    data1.deviceId = listfrontend.deviceId;
                    data1.displayTime = listfrontend.displayTime;
                    data1.stationNumber = listfrontend.stationNumber;
                    data1.timestamp = listfrontend.timestamp;
                    int i = bll.Add(listfrontend);
                    bllValue.Add(data1);
                }
            }
        }

        public class Rootdr
        {
            /// <summary>
            /// 当前分页页数
            /// </summary>
            public int currentPage { get; set; }
            /// <summary>
            /// 排序的字段  如 timestamp
            /// </summary>
            public string orderBy { get; set; }
            /// <summary>
            /// desc | asc
            /// </summary>
            public string orderDirection { get; set; }
            /// <summary>
            /// 自定义分页数 默认20
            /// </summary>
            public int customizedPageSize { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string sortOrders { get; set; }
            /// <summary>
            /// 站号 （必填）
            /// </summary>
            public int stationNumber { get; set; }
            /// <summary>
            /// 开始时间戳
            /// </summary>
            public long start { get; set; }
            /// <summary>
            /// 结束时间戳
            /// </summary>
            public long end { get; set; }
        }

        public class Rootdr1
        {
            /// <summary>
            /// 当前分页页数
            /// </summary>
            public string date { get; set; }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            DateTime dateTime = DateTime.Today; // 获取当前UTC时间
            long timestamp = new DateTimeOffset(dateTime).ToUnixTimeMilliseconds();

            DateTime now = DateTime.Now; // 获取当前时间
            DateTime startOfDay = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0); // 当天的开始时间
            DateTime endOfLastHour = startOfDay.AddHours(23).AddMinutes(59); // 当天最后一个小时的结束时间
            // 如果你需要的是开始时间而不是结束时间，可以减去59分钟再减去1小时
            DateTime startOfLastHour = endOfLastHour.AddMinutes(-19).AddHours(-1);
            long timestamp1 = new DateTimeOffset(startOfLastHour).ToUnixTimeMilliseconds();

            var aaaaa = DateTime.Now.Minute;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            HttpClient Client = new HttpClient();

            List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
            List<string> listurl = new List<string>();

            var headers = new Dictionary<string, string>
            {
                { "Authorization", "FN6/jhuULbmg3IFolczTsNI6XpWJeRPeiKQYDe3kCzR6WBVC6KwsJTAnkJHeBmouwMQDxgd0pgpU7/qUgYHZQA==" }
            };
            var headers1 = new Dictionary<string, string>
            {
                { "Authorization", "a1FX77Et8yPDUBzdElQ7iOU47EFmJG25JNKISyrgePPMkBVoKNEV2mqBntc4i5U1uwIK87aQzW8aKWgFjsYw5A==" }
            };
            list.Add(headers);
            list.Add(headers1);

            string reportUrl = "http://192.168.22.250:10010/create-virtual-data/details";
            string reportUrl1 = "http://192.168.22.248:10010/create-virtual-data/details";

            listurl.Add(reportUrl);
            listurl.Add(reportUrl1);


            //JsonSerializerSettings settings = new JsonSerializerSettings
            //{
            //    NullValueHandling = NullValueHandling.Ignore,
            //    DefaultValueHandling = DefaultValueHandling.Ignore
            //};

            //Maticsoft.BLL.TypeValue bllValue = new Maticsoft.BLL.TypeValue();
            for (int ii = 0; ii < list.Count; ii++)
            {



                Rootdr1 paramsObject = new Rootdr1
                {
                    date = DateTime.Today.AddDays(-0).ToString("yyyy-MM-dd"),

                };

                string createRetJson = HttpClientUtils.DoJsonPost(paramsObject, listurl[ii], list[ii]);





                Root1 data = JsonConvert.DeserializeObject<Root1>(createRetJson);//JSON 转成实体

                if (data.code == 0 && data.result != null)
                {


                    //Maticsoft.Model.TypeValue data1 = new Maticsoft.Model.TypeValue();

                    Maticsoft.BLL.QFQCType bllType = new Maticsoft.BLL.QFQCType();
                    Maticsoft.BLL.QFQCson bll = new Maticsoft.BLL.QFQCson();
                    DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(data.result.lastTime);
                    DateTime localDateTime = dateTimeOffset.LocalDateTime;
                    int count = bll.Delete("  FS08 ='" + data.result.line + "'  and FS09 ='" + data.result.customer + "' and FS10='" + data.result.model + "'  AND  MODT ='" + Convert.ToDateTime(localDateTime).Date + "'  ");

                    Maticsoft.Model.QFQCson QFQCson = new Maticsoft.Model.QFQCson();
                    PropertyInfo[] properties = data.result.defictive.GetType().GetProperties();

                    for (int i = 0; i < properties.Length; i++)
                    {
                        QFQCson = new Maticsoft.Model.QFQCson();
                        string propertyName = properties[i].Name; // 如 "PIN低", "PIN高"
                        object propertyValue = properties[i].GetValue(data.result.defictive);


                        QFQCson.CRT = Convert.ToDateTime(localDateTime);

                        QFQCson.CRP = "自动化采集";
                        QFQCson.MODT = QFQCson.CRT.Date;

                        QFQCson.FS02 = propertyName;
                        QFQCson.FS03 = Convert.ToInt32(propertyValue);
                        QFQCson.FS08 = data.result.line;
                        QFQCson.FS09 = data.result.customer;
                        QFQCson.FS10 = data.result.model;

                        QFQCson.FS20 = data.result.totalplan; //计划总数据
                        QFQCson.FS13 = data.result.leader; //组长
                        QFQCson.FS21 = data.result.totalresidue; //剩余完成量


                        var type = bllType.GetList(" name ='" + propertyName + "' ");

                        if (type.Tables[0].Rows.Count != 0)
                        {
                            QFQCson.FS07 = type.Tables[0].Rows[0]["Value"].ToString();

                        }
                        else
                        {
                            QFQCson.FS07 = "其它";
                        }
                        if (propertyName == "加工数")
                        {
                            QFQCson.FS12 = Convert.ToInt32(propertyValue);
                            QFQCson.FS07 = "生产总数";

                        }
                        int iii = bll.Add(QFQCson);
                    }

                    //string aaa = DateTime.Now.AddDays(-0).ToString("yyyy-MM-dd");
                    //bool ii = bll.Delete(aaa, item);
                    //bool delete = bllValue.Delete1(aaa, item);


                    //listfrontend.accumulateVolume = Convert.ToDecimal(data.results[0].accumulateVolume);
                    //listfrontend.electricityCosume = Convert.ToDecimal(data.results[0].electricityCosume);
                    //listfrontend.code = data.code;
                    //listfrontend.message = data.message;
                    //listfrontend.stationNumber = data.pager.stationNumber;
                    //listfrontend.timestamp = data.results[0].timestamp;
                    //long timestamp = data.results[0].timestamp;
                    //DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
                    //DateTime localDateTime = dateTimeOffset.LocalDateTime;
                    //listfrontend.displayTime = Convert.ToDateTime(localDateTime);
                    //listfrontend.count = data.results[0].count;
                    //listfrontend.vendor = data.results[0].vendor;
                    //listfrontend.bitPart = data.results[0].bitPart;
                    //listfrontend.deviceId = data.results[0].deviceId;
                    //listfrontend.valueMapping = data.results[0].valueMapping.ToJSON();

                    // data1 = JsonConvert.DeserializeObject<Maticsoft.Model.TypeValue>(listfrontend.valueMapping, settings);

                    //data1.count = listfrontend.count;
                    //data1.deviceId = listfrontend.deviceId;
                    //data1.displayTime = listfrontend.displayTime;
                    //data1.stationNumber = listfrontend.stationNumber;
                    //data1.timestamp = listfrontend.timestamp;
                    //int i = bll.Add(listfrontend);
                    //bllValue.Add(data1);
                }
            }

        }
    }
}
